/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.enums.AbnormalEnum;
import com.pig4cloud.pigx.common.customize.exception.DuckweedException;
import com.pig4cloud.pigx.common.customize.security.utils.SecurityUtil;
import com.pig4cloud.pigx.common.customize.utils.id.IdWorker;
import com.pig4cloud.pigx.common.file.core.FileTemplate;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import com.pig4cloud.pigx.user.entity.UserAgreement;
import com.pig4cloud.pigx.user.mapper.UserAgreementMapper;
import com.pig4cloud.pigx.user.service.UserAgreementService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户合同签订
 *
 * @author pigx code generator
 * @date 2022-12-26 09:52:59
 */
@Service
@AllArgsConstructor
public class UserAgreementServiceImpl extends ServiceImpl<UserAgreementMapper, UserAgreement> implements UserAgreementService {

	private final IdWorker idWorker;

	private final FileTemplate template;

	/**
	 * 签署合同上传
	 * @param file 合同图片
	 * @return r
	 */
	@Override
	public R<String> saveAgreement(MultipartFile file) {
		// 获取登录用户
		PigxUser user = SecurityUtil.getLoginUser();
		if (user == null) {
			throw new DuckweedException(AbnormalEnum.MSG.OPTIONS_USER_NOT_FOUND);
		}
		long selectCount = new UserAgreement().selectCount(new QueryWrapper<UserAgreement>() {{
			eq("user_id", user.getId());
		}});
		if (selectCount > 0) {
			throw new DuckweedException(AbnormalEnum.MSG.UNSUPPORTED_OPERATION.getCode(),"请勿重复签名");
		}
		String fileName = "agreement/" + user.getId();
		/*try {
			template.putObject("duckweedexchanges", fileName, file.getInputStream(), file.getContentType());
		} catch (Exception e) {
			throw new DuckweedException(AbnormalEnum.MSG.DISCONNECTED.getCode(),"合同上传失败");
		}*/
		UserAgreement agreement = new UserAgreement();
		agreement.setId(idWorker.nextId());
		agreement.setUserId(user.getId());
		String url = "https://duckweedexchanges.oss-cn-chengdu.aliyuncs.com/";
		agreement.setAgreementImage(url + fileName);
		agreement.insert();
		return R.ok("合同上传成功!");
	}

}
